from os.path import dirname, abspath

from utils.write_log_to_file import Logger
import time


def exec_check(fn, site_id,url, re_throw = False):
    root_dir = dirname(dirname(abspath(__file__)))
    print(root_dir)
    log = Logger(f'{root_dir}/x2_migration.log', level='debug')
    logerr = Logger(f'{root_dir}/x2_migration_err.log', level='error')
    try:
        log.logger.debug("执行了%s方法,site_id = %s,url = %s", fn.__name__, site_id, url)
        fn()
        time.sleep(3)
    except Exception as e:
        logerr.logger.error("%s方法执行失败,site_id = %s,url = %s", fn.__name__,site_id,url)
        logerr.logger.error("异常信息为：", e)
        if re_throw:
            raise e

    log.logger.removeHandler(log.sh)
    log.logger.removeHandler(log.th)
    logerr.logger.removeHandler(logerr.sh)
    logerr.logger.removeHandler(logerr.th)


